package com.asynctm.android.socialnetwork.dao;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;

import com.asynctm.android.common.exception.DaoException;
import com.asynctm.android.socialnetwork.dto.ContactDto;
import com.google.inject.Inject;

/**
 * Implementa {@link ContactDao}
 * 
 * @author rockdrigo
 */
public class ContactDaoImpl implements ContactDao {

	private ContentResolver contentResolver;

	@Inject
	public ContactDaoImpl(Context context) {
		this.contentResolver = context.getContentResolver();
	}

	public List<ContactDto> getContactsWithPhonesAndEmails()
			throws DaoException {
		List<ContactDto> contactList = new ArrayList<ContactDto>();
		String[] projection = new String[] { ContactsContract.Contacts._ID,
				ContactsContract.Contacts.DISPLAY_NAME,
				ContactsContract.Contacts.PHOTO_ID };
		Cursor curContacts = contentResolver.query(
				ContactsContract.Contacts.CONTENT_URI, projection, null, null,
				ContactsContract.Contacts.DISPLAY_NAME);
		curContacts.moveToFirst();
		while (!curContacts.isAfterLast()) {
			String currentContactId = curContacts.getString(curContacts
					.getColumnIndex(ContactsContract.Contacts._ID));
			ContactDto currentContact = new ContactDto(
					currentContactId,
					curContacts.getString(curContacts
							.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)),
					getPhoneList(currentContactId),
					getEmailList(currentContactId), getPhoto(currentContactId));
			contactList.add(currentContact);
			curContacts.moveToNext();
		}
		curContacts.close();
		return contactList;
	}

	public List<String> getEmailList(String contactId) throws DaoException {
		List<String> emails = new ArrayList<String>();

		Cursor curEmails = contentResolver.query(
				ContactsContract.CommonDataKinds.Email.CONTENT_URI,
				new String[] { ContactsContract.CommonDataKinds.Email.DATA },
				ContactsContract.Data.CONTACT_ID + " = " + contactId, null,
				null);

		curEmails.moveToFirst();
		while (!curEmails.isAfterLast()) {
			emails.add(curEmails.getString(curEmails
					.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
			curEmails.moveToNext();
		}

		curEmails.close();
		return emails;
	}

	public List<String> getPhoneList(String contactId) throws DaoException {
		List<String> phones = new ArrayList<String>();

		Cursor curPhones = contentResolver.query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
				new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER },
				ContactsContract.Data.CONTACT_ID + " = " + contactId, null,
				null);

		curPhones.moveToFirst();
		while (!curPhones.isAfterLast()) {
			phones.add(curPhones.getString(curPhones
					.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
			curPhones.moveToNext();
		}

		curPhones.close();
		return phones;
	}

	public Bitmap getPhoto(String contactId) {
		Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
				Integer.parseInt(contactId));
		Uri photoUri = Uri.withAppendedPath(contactUri,
				Contacts.Photo.CONTENT_DIRECTORY);
		Cursor cursor = contentResolver.query(photoUri,
				new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO },
				null, null, null);
		if (cursor == null) {
			return null;
		}
		try {
			if (cursor.moveToFirst()) {
				byte[] data = cursor.getBlob(0);
				if (data != null) {
					return BitmapFactory.decodeStream(new ByteArrayInputStream(
							data));
				}
			}
		} finally {
			cursor.close();
		}
		return null;
	}

}
